<?php
namespace Home\Controller;
use Think\Controller;
use Home\Service\ClockTimeService;
use Home\Service\ResetDataService;
use Home\Service\WechatService;
use Think\Cache\Driver\Redis;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年1月23日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class LoaderController extends Controller{
    
    protected $errMsg = '';
    protected $errCode = 0;
    private function getError(){
        return ['errcode'=>$this->errCode,'errmsg'=>$this->errMsg];
    }
    
        
    
    /**
     * 考勤机发送数据前读取的配置信息
     * 
     * date:2017年2月9日
     * author: EK_熊
     */
    public function config(){
       $devcode = I('dev_code');
    //    $devcode = $devcode ? "att_cfg_$devcode" : 'dev_loader'; 
        $data = D('Options')->getOption("att_cfg_$devcode");
        $this->ajaxReturn($data);
    }
    
    
    /**
     * 考勤机发送考勤数据接收对方开放入口
     * errocode :1+
     * date:2017年2月9日
     * author: EK_熊
     */
    public function send(){
        $attendance = htmlspecialchars_decode(I('data'));
        D('LoaderRequest')->addLog($attendance);//把发送过来的数据保存一下，方便调试查看
        $attendance = json_decode($attendance,true);
        
        if (C('DEBUG_LOADER_DATA')){
        //    $attendance = $this->analogData();//数据模拟
        }
        
        
        $ret = $this->attendance($attendance);//执行数据操作
        
        if ($ret['errCode'] == 0) {
            $ret['count'] = count($attendance);
        }
        $this->ajaxReturn($ret);
    }
    
    
    /**
     * 考勤数据处理的核心代码
     * @param string $attendance  接收数组格式
     * date:2017年2月21日
     * author: EK_熊
     */
    public function attendance($attendance=[],$auto=true){
        $attenDayModel = D('AttendanceDay');
        do{
            /*来源数据检查*/
            if (!$attendance){
                $this->errCode=1;
                $this->errMsg='接收不到参数或者参数格式错误';
                break;
            }
        
            /*从员工数据平台获取员工信息*/
            $attendanceUserInfo= D('Home/Account','Service')->append_user_field('sn',$attendance,'sn,dep_id,dep_name,ep_id as uid,ep_name as name');
            $collectDay = $attenDayModel->collect_day($attendanceUserInfo,$auto);
            if (!$collectDay){
                $this->errCode=3;
                $this->errMsg=$attenDayModel->getError();
                break;
            }
        }while(false);
        if ($this->errMsg) D('LoaderRequest')->addLog($this->errMsg);//记录请求返回     
        return $this->getError();
    }
    
    
    /**
     * 输出前一天的打卡记录总数
     * 
     * date:2017年9月20日
     * author: EK_熊
     */
    public function total_prev(){
        $day = I('day');
        //$day = $day ? $day : date('Y-m-d',(time()-86400));
        $map['recept_time']=[['egt',"$day 00:00:00"],['elt',"$day 23:59:59"]];
        $count = M('Attendance')->where($map)->count();
        $data['count']=$count;
        $this->ajaxReturn($data);
        
    }
    
    /**
     * 接收对比处理结果并进行保存
     * 
     * date:2017年9月20日
     * author: EK_熊
     */
    public function total_prev_result(){
        $result = I('result');
        $code = I('code');
        $add['info'] = $result;
        $add['created_time'] = CURRENT_TIME;
        $add['dev_code'] = I('dev_code');
        M('AttendanceChecklog')->add($add);
        
        //
        if ($code !== 0){
            //发送微信 报警
        }
    }
    
    
    /**
     * 自动修复功能：1.处理数据差集处理
     * 把差集数据放进缓存，方便失败之后可以再次运行
     * date:2017年10月13日
     * author: EK_熊
     */
    public function repair_diff(){
        $totalid = urldecode(I('ids'));//当天的全部记录id
        $date =I('date'); //日期
        $dev_code = I('dev_code');//设备编号
        
        $cache_key ="repair_{$dev_code}_{$date}"; 
        $cache_repair = F($cache_key);
        if (!$cache_repair) {
            
            $machine_id = json_decode($totalid,true);
            //提取前一天接收到的全部打卡数据
            $map['recept_time']=[['egt',"$date 00:00:00"],['elt',"$date 23:59:59"]];
            $map['dev_code'] = ['like',"%$dev_code%"];
            $count = M('Attendance')->where($map)->getField('dev_id',true);
            $count = $count== null ? [] : $count;
            //计算差集
            $diff = array_diff($machine_id,$count);
            F($cache_key,$diff);
        }else{
            $diff = $cache_repair;
        }
        
        $this->ajaxReturn($diff);
    }
    
    /**
     * 自动修复功能：2.接收并且添加差集数据
     * 
     * date:2017年10月12日
     * author: EK_熊
     */
    public function repair_add(){
        $attendance = json_decode(urldecode(I('data')),true);
        
        $date = I('date');//需要处理数据的日期
        $devcode = I('dev_code');
        $cache_key ="repair_{$devcode}_{$date}";//缓存名称
        
//         $attendance = '[{"dev_id":"804441","sn":"1041","time":"2017-11-06 02:17:11","name":"\u67ef\u51ef\u6587","dev_code":"fzzx_1","row_number":"1"},{"dev_id":"804442","sn":"10038","time":"2017-11-06 03:03:41","name":null,"dev_code":"fzzx_1","row_number":"2"},{"dev_id":"804443","sn":"0863","time":"2017-11-06 03:03:43","name":"\u502a\u67ab\u7389","dev_code":"fzzx_2","row_number":"3"},{"dev_id":"804444","sn":"0154","time":"2017-11-06 03:03:47","name":"\u5218\u82e5\u6d0b","dev_code":"fzzx_2","row_number":"4"},{"dev_id":"804446","sn":"0800","time":"2017-11-06 03:04:30","name":"\u8bb8\u53ef\u626c","dev_code":"fzzx_2","row_number":"5"},{"dev_id":"804447","sn":"0614","time":"2017-11-06 03:04:47","name":"\u674e\u56fd\u826f","dev_code":"fzzx_1","row_number":"6"},{"dev_id":"804450","sn":"0197","time":"2017-11-06 07:30:52","name":"\u6797\u519b","dev_code":"fzzx_2","row_number":"7"},{"dev_id":"804454","sn":"0669","time":"2017-11-06 07:34:12","name":"\u5ed6\u9732\u9732","dev_code":"fzzx_2","row_number":"8"},{"dev_id":"804456","sn":"1113","time":"2017-11-06 07:51:31","name":"\u5434\u7d20\u8d24","dev_code":"fzzx_2","row_number":"9"},{"dev_id":"804457","sn":"10188","time":"2017-11-06 08:07:11","name":null,"dev_code":"fzzx_2","row_number":"10"},{"dev_id":"804459","sn":"1096","time":"2017-11-06 08:14:46","name":"\u8d75\u5f71","dev_code":"fzzx_2","row_number":"11"},{"dev_id":"804460","sn":"10183","time":"2017-11-06 08:39:19","name":null,"dev_code":"fzzx_2","row_number":"12"},{"dev_id":"804461","sn":"10164","time":"2017-11-06 08:42:04","name":null,"dev_code":"fzzx_2","row_number":"13"},{"dev_id":"804462","sn":"0647","time":"2017-11-06 08:45:04","name":null,"dev_code":"fzzx_2","row_number":"14"},{"dev_id":"804463","sn":"1029","time":"2017-11-06 08:48:21","name":"\u5f20\u5f66","dev_code":"fzzx_2","row_number":"15"},{"dev_id":"804464","sn":"0769","time":"2017-11-06 08:48:36","name":"\u9ec4\u5317\u5149","dev_code":"fzzx_1","row_number":"16"},{"dev_id":"804465","sn":"10173","time":"2017-11-06 08:50:03","name":null,"dev_code":"fzzx_2","row_number":"17"},{"dev_id":"804466","sn":"1053","time":"2017-11-06 08:52:46","name":"\u4f55\u4fca\u8f89\r\n","dev_code":"fzzx_2","row_number":"18"},{"dev_id":"804467","sn":"0615","time":"2017-11-06 08:52:54","name":"Anderson Richard Alexander","dev_code":"fzzx_2","row_number":"19"},{"dev_id":"804468","sn":"10187","time":"2017-11-06 08:52:58","name":null,"dev_code":"fzzx_1","row_number":"20"},{"dev_id":"804470","sn":"1028","time":"2017-11-06 09:03:58","name":"\u6b66\u6c38\u4e50","dev_code":"fzzx_1","row_number":"21"},{"dev_id":"804471","sn":"10131","time":"2017-11-06 09:04:13","name":null,"dev_code":"fzzx_2","row_number":"22"},{"dev_id":"804472","sn":"10155","time":"2017-11-06 09:04:22","name":null,"dev_code":"fzzx_2","row_number":"23"},{"dev_id":"804473","sn":"0012","time":"2017-11-06 09:06:58","name":"\u949f\u5a07","dev_code":"fzzx_2","row_number":"24"},{"dev_id":"804474","sn":"0859","time":"2017-11-06 09:07:07","name":"\u4f58\u5955\u6977","dev_code":"fzzx_1","row_number":"25"},{"dev_id":"804476","sn":"0904","time":"2017-11-06 09:09:01","name":"\u5b54\u5609\u7389","dev_code":"fzzx_2","row_number":"26"},{"dev_id":"804477","sn":"0560","time":"2017-11-06 09:11:28","name":"\u7a0b\u4f1f","dev_code":"fzzx_2","row_number":"27"},{"dev_id":"804479","sn":"10175","time":"2017-11-06 09:12:06","name":null,"dev_code":"fzzx_1","row_number":"28"},{"dev_id":"804481","sn":"10174","time":"2017-11-06 09:14:14","name":null,"dev_code":"fzzx_2","row_number":"29"},{"dev_id":"804482","sn":"10160","time":"2017-11-06 09:14:18","name":null,"dev_code":"fzzx_1","row_number":"30"},{"dev_id":"804483","sn":"0968","time":"2017-11-06 09:16:10","name":"\u6768\u6625\u5b87","dev_code":"fzzx_2","row_number":"31"},{"dev_id":"804484","sn":"0011","time":"2017-11-06 09:17:50","name":"\u8c2d\u7433","dev_code":"fzzx_2","row_number":"32"},{"dev_id":"804485","sn":"10158","time":"2017-11-06 09:19:03","name":null,"dev_code":"fzzx_1","row_number":"33"},{"dev_id":"804487","sn":"10019","time":"2017-11-06 09:19:41","name":null,"dev_code":"fzzx_2","row_number":"34"},{"dev_id":"804488","sn":"10184","time":"2017-11-06 09:19:50","name":null,"dev_code":"fzzx_1","row_number":"35"},{"dev_id":"804490","sn":"1073","time":"2017-11-06 09:21:35","name":"\u6613\u5e06","dev_code":"fzzx_2","row_number":"36"},{"dev_id":"804491","sn":"1049","time":"2017-11-06 09:22:47","name":"\u4f55\u6d01\u83b9","dev_code":"fzzx_2","row_number":"37"},{"dev_id":"804492","sn":"0911","time":"2017-11-06 09:22:56","name":"\u6731\u9896","dev_code":"fzzx_1","row_number":"38"},{"dev_id":"804493","sn":"10020","time":"2017-11-06 09:22:55","name":null,"dev_code":"fzzx_2","row_number":"39"},{"dev_id":"804494","sn":"10035","time":"2017-11-06 09:23:08","name":null,"dev_code":"fzzx_2","row_number":"40"},{"dev_id":"804495","sn":"0169","time":"2017-11-06 09:23:59","name":"\u9648\u5bb6\u6d69","dev_code":"fzzx_1","row_number":"41"},{"dev_id":"804497","sn":"1112","time":"2017-11-06 09:25:43","name":"\u5434\u6842\u73ca","dev_code":"fzzx_2","row_number":"42"},{"dev_id":"804499","sn":"10181","time":"2017-11-06 09:25:48","name":null,"dev_code":"fzzx_2","row_number":"43"},{"dev_id":"804500","sn":"0572","time":"2017-11-06 09:27:00","name":"\u5218\u4e3d","dev_code":"fzzx_2","row_number":"44"},{"dev_id":"804501","sn":"1016","time":"2017-11-06 09:28:17","name":"\u51af\u6d77\u5bb9","dev_code":"fzzx_1","row_number":"45"},{"dev_id":"804502","sn":"0345","time":"2017-11-06 09:28:20","name":"\u5218\u82e5\u8335","dev_code":"fzzx_1","row_number":"46"},{"dev_id":"804503","sn":"0242","time":"2017-11-06 09:29:29","name":"\u66f9\u8587","dev_code":"fzzx_1","row_number":"47"},{"dev_id":"804504","sn":"0674","time":"2017-11-06 09:29:31","name":"\u80e1\u6587\u5a1f","dev_code":"fzzx_1","row_number":"48"},{"dev_id":"804505","sn":"1110","time":"2017-11-06 09:30:09","name":"\u674e\u6620\u6625","dev_code":"fzzx_2","row_number":"49"},{"dev_id":"804506","sn":"10143","time":"2017-11-06 09:30:15","name":null,"dev_code":"fzzx_2","row_number":"50"},{"dev_id":"804507","sn":"0627","time":"2017-11-06 09:32:16","name":"\u5f20\u96c5\u8389","dev_code":"fzzx_2","row_number":"51"},{"dev_id":"804508","sn":"0039","time":"2017-11-06 09:32:46","name":"\u8463\u4e07\u519b","dev_code":"fzzx_2","row_number":"52"},{"dev_id":"804509","sn":"0003","time":"2017-11-06 09:32:53","name":"\u5434\u5b8f\u5c71","dev_code":"fzzx_1","row_number":"53"},{"dev_id":"804510","sn":"0571","time":"2017-11-06 09:32:55","name":"\u738b\u6653\u71a0","dev_code":"fzzx_2","row_number":"54"},{"dev_id":"804511","sn":"1084","time":"2017-11-06 09:32:58","name":"\u5f20\u5146\u7965","dev_code":"fzzx_1","row_number":"55"},{"dev_id":"804512","sn":"10013","time":"2017-11-06 09:34:24","name":null,"dev_code":"fzzx_2","row_number":"56"},{"dev_id":"804513","sn":"10129","time":"2017-11-06 09:34:33","name":null,"dev_code":"fzzx_1","row_number":"57"},{"dev_id":"804514","sn":"0905","time":"2017-11-06 09:34:41","name":"\u6c5f\u6167\u83b9","dev_code":"fzzx_1","row_number":"58"},{"dev_id":"804516","sn":"1082","time":"2017-11-06 09:34:45","name":"\u5f20\u8212\u5ca9","dev_code":"fzzx_2","row_number":"59"},{"dev_id":"804517","sn":"10141","time":"2017-11-06 09:35:06","name":null,"dev_code":"fzzx_2","row_number":"60"},{"dev_id":"804518","sn":"10192","time":"2017-11-06 09:35:15","name":null,"dev_code":"fzzx_1","row_number":"61"},{"dev_id":"804519","sn":"10182","time":"2017-11-06 09:35:14","name":null,"dev_code":"fzzx_2","row_number":"62"},{"dev_id":"804520","sn":"10041","time":"2017-11-06 09:35:19","name":null,"dev_code":"fzzx_1","row_number":"63"},{"dev_id":"804521","sn":"1042","time":"2017-11-06 09:36:24","name":"\u9ece\u5de7\u6021","dev_code":"fzzx_2","row_number":"64"},{"dev_id":"804522","sn":"1068","time":"2017-11-06 09:36:29","name":"\u8d75\u8c6a\u5e05","dev_code":"fzzx_1","row_number":"65"},{"dev_id":"804523","sn":"0713","time":"2017-11-06 09:38:31","name":"\u8c2d\u5065\u96ef","dev_code":"fzzx_2","row_number":"66"},{"dev_id":"804524","sn":"0346","time":"2017-11-06 09:38:43","name":"\u8c2d\u4e3d\u71d5","dev_code":"fzzx_2","row_number":"67"},{"dev_id":"804525","sn":"10054","time":"2017-11-06 09:38:51","name":null,"dev_code":"fzzx_1","row_number":"68"},{"dev_id":"804526","sn":"10144","time":"2017-11-06 09:39:54","name":null,"dev_code":"fzzx_2","row_number":"69"},{"dev_id":"804527","sn":"10003","time":"2017-11-06 09:41:16","name":null,"dev_code":"fzzx_1","row_number":"70"},{"dev_id":"804528","sn":"0796","time":"2017-11-06 09:41:17","name":"\u51af\u5fd7\u950b","dev_code":"fzzx_2","row_number":"71"},{"dev_id":"804529","sn":"0446","time":"2017-11-06 09:41:21","name":"\u6768\u654f","dev_code":"fzzx_1","row_number":"72"},{"dev_id":"804530","sn":"10090","time":"2017-11-06 09:42:02","name":null,"dev_code":"fzzx_2","row_number":"73"},{"dev_id":"804531","sn":"10156","time":"2017-11-06 09:42:17","name":null,"dev_code":"fzzx_1","row_number":"74"},{"dev_id":"804533","sn":"10034","time":"2017-11-06 09:44:17","name":null,"dev_code":"fzzx_2","row_number":"75"},{"dev_id":"804534","sn":"1001","time":"2017-11-06 09:44:20","name":"\u949f\u666f\u65bd","dev_code":"fzzx_1","row_number":"76"},{"dev_id":"804535","sn":"10157","time":"2017-11-06 09:44:41","name":null,"dev_code":"fzzx_2","row_number":"77"},{"dev_id":"804536","sn":"10136","time":"2017-11-06 09:44:45","name":null,"dev_code":"fzzx_2","row_number":"78"},{"dev_id":"804537","sn":"0009","time":"2017-11-06 09:45:09","name":"\u9648\u6d69","dev_code":"fzzx_1","row_number":"79"},{"dev_id":"804538","sn":"0820","time":"2017-11-06 09:45:43","name":"\u9ec4\u654f\u73b2","dev_code":"fzzx_2","row_number":"80"},{"dev_id":"804539","sn":"0738","time":"2017-11-06 09:46:22","name":"\u6797\u4f1f\u8c6a","dev_code":"fzzx_2","row_number":"81"},{"dev_id":"804540","sn":"1027","time":"2017-11-06 09:47:37","name":"\u9648\u4e16\u559c","dev_code":"fzzx_2","row_number":"82"},{"dev_id":"804541","sn":"0691","time":"2017-11-06 09:47:46","name":"\u7530\u8273","dev_code":"fzzx_1","row_number":"83"},{"dev_id":"804542","sn":"0328","time":"2017-11-06 09:49:07","name":"\u59da\u5fd7\u534e","dev_code":"fzzx_2","row_number":"84"},{"dev_id":"804543","sn":"0846","time":"2017-11-06 09:49:12","name":"\u5468\u5929\u5ad2","dev_code":"fzzx_1","row_number":"85"},{"dev_id":"804544","sn":"10039","time":"2017-11-06 09:49:12","name":null,"dev_code":"fzzx_2","row_number":"86"},{"dev_id":"804545","sn":"0708","time":"2017-11-06 09:49:17","name":"\u5f20\u57a0\u6d01","dev_code":"fzzx_1","row_number":"87"},{"dev_id":"804546","sn":"10022","time":"2017-11-06 09:49:20","name":null,"dev_code":"fzzx_1","row_number":"88"},{"dev_id":"804547","sn":"0945","time":"2017-11-06 09:50:25","name":"\u6797\u5a77","dev_code":"fzzx_2","row_number":"89"},{"dev_id":"804548","sn":"10072","time":"2017-11-06 09:50:55","name":null,"dev_code":"fzzx_2","row_number":"90"},{"dev_id":"804549","sn":"10092","time":"2017-11-06 09:52:28","name":null,"dev_code":"fzzx_2","row_number":"91"},{"dev_id":"804550","sn":"1052","time":"2017-11-06 09:53:03","name":"\u5218\u6d8c","dev_code":"fzzx_2","row_number":"92"},{"dev_id":"804551","sn":"10159","time":"2017-11-06 09:53:06","name":null,"dev_code":"fzzx_1","row_number":"93"},{"dev_id":"804552","sn":"1086","time":"2017-11-06 09:55:00","name":"\u8d3a\u601d\u6d0b","dev_code":"fzzx_2","row_number":"94"},{"dev_id":"804553","sn":"1098","time":"2017-11-06 09:55:03","name":"\u9648\u4f1f\u5cf0","dev_code":"fzzx_1","row_number":"95"},{"dev_id":"804554","sn":"0896","time":"2017-11-06 09:55:05","name":"\u6d2a\u5fd7\u7fa4","dev_code":"fzzx_2","row_number":"96"},{"dev_id":"804555","sn":"0045","time":"2017-11-06 09:55:08","name":"\u5218\u8273","dev_code":"fzzx_1","row_number":"97"},{"dev_id":"804556","sn":"1088","time":"2017-11-06 09:55:07","name":"\u9ec4\u535a\u8a89","dev_code":"fzzx_2","row_number":"98"},{"dev_id":"804558","sn":"0001","time":"2017-11-06 09:55:31","name":"\u674e\u660e","dev_code":"fzzx_2","row_number":"99"},{"dev_id":"804559","sn":"0838","time":"2017-11-06 09:56:15","name":"\u90b1\u6822\u9526","dev_code":"fzzx_2","row_number":"100"},{"dev_id":"804560","sn":"0639","time":"2017-11-06 09:56:18","name":"\u5218\u6d0b","dev_code":"fzzx_1","row_number":"101"},{"dev_id":"804561","sn":"1014","time":"2017-11-06 09:56:18","name":"\u5218\u5bcc\u68ee","dev_code":"fzzx_2","row_number":"102"},{"dev_id":"804562","sn":"0880","time":"2017-11-06 09:57:31","name":"\u5360\u5b5d\u864e","dev_code":"fzzx_2","row_number":"103"},{"dev_id":"804563","sn":"10042","time":"2017-11-06 09:57:37","name":null,"dev_code":"fzzx_1","row_number":"104"},{"dev_id":"804564","sn":"0837","time":"2017-11-06 09:57:35","name":"\u9648\u5b50\u60e0","dev_code":"fzzx_2","row_number":"105"},{"dev_id":"804565","sn":"0017","time":"2017-11-06 09:57:46","name":"\u7f57\u5a75","dev_code":"fzzx_2","row_number":"106"},{"dev_id":"804566","sn":"10179","time":"2017-11-06 09:58:17","name":null,"dev_code":"fzzx_1","row_number":"107"},{"dev_id":"804567","sn":"1035","time":"2017-11-06 09:59:17","name":"\u5f20\u7f8e\u742a","dev_code":"fzzx_2","row_number":"108"},{"dev_id":"804568","sn":"0522","time":"2017-11-06 10:00:40","name":"\u5f20\u4e3d\u4e3d","dev_code":"fzzx_2","row_number":"109"},{"dev_id":"804569","sn":"0153","time":"2017-11-06 10:00:43","name":"\u5ed6\u7f8e\u7075","dev_code":"fzzx_2","row_number":"110"},{"dev_id":"804570","sn":"0626","time":"2017-11-06 10:00:48","name":"\u9ec4\u4f69\u8bd7","dev_code":"fzzx_1","row_number":"111"},{"dev_id":"804571","sn":"0907","time":"2017-11-06 10:00:53","name":"\u65b9\u5ce5","dev_code":"fzzx_1","row_number":"112"},{"dev_id":"804572","sn":"0075","time":"2017-11-06 10:02:00","name":"\u949f\u79d1\u5b66","dev_code":"fzzx_2","row_number":"113"},{"dev_id":"804573","sn":"0297","time":"2017-11-06 10:02:08","name":"\u4f59\u82b3","dev_code":"fzzx_1","row_number":"114"},{"dev_id":"804574","sn":"0405","time":"2017-11-06 10:03:51","name":"\u9648\u857e\u857e","dev_code":"fzzx_2","row_number":"115"},{"dev_id":"804575","sn":"1039","time":"2017-11-06 10:04:04","name":"\u9648\u5353\u6021","dev_code":"fzzx_2","row_number":"116"},{"dev_id":"804576","sn":"0016","time":"2017-11-06 10:05:14","name":"\u5468\u6676\u6676","dev_code":"fzzx_2","row_number":"117"},{"dev_id":"804577","sn":"10152","time":"2017-11-06 10:05:21","name":null,"dev_code":"fzzx_1","row_number":"118"},{"dev_id":"804578","sn":"0138","time":"2017-11-06 10:05:25","name":"\u8c46\u9759\u9759","dev_code":"fzzx_1","row_number":"119"},{"dev_id":"804579","sn":"0903","time":"2017-11-06 10:05:28","name":"\u5468\u5999\u73b2","dev_code":"fzzx_1","row_number":"120"},{"dev_id":"804580","sn":"1025","time":"2017-11-06 10:05:55","name":"\u738b\u6cfd\u6602","dev_code":"fzzx_2","row_number":"121"},{"dev_id":"804581","sn":"0072","time":"2017-11-06 10:07:37","name":"\u4e25\u806a","dev_code":"fzzx_2","row_number":"122"},{"dev_id":"804582","sn":"10151","time":"2017-11-06 10:08:25","name":null,"dev_code":"fzzx_2","row_number":"123"},{"dev_id":"804583","sn":"0130","time":"2017-11-06 10:08:28","name":"\u66fe\u9896\u83b9","dev_code":"fzzx_1","row_number":"124"},{"dev_id":"804584","sn":"0716","time":"2017-11-06 10:10:18","name":"\u90d1\u5251\u660e","dev_code":"fzzx_2","row_number":"125"},{"dev_id":"804585","sn":"0041","time":"2017-11-06 10:10:23","name":"\u674e\u52a0\u6b23","dev_code":"fzzx_2","row_number":"126"},{"dev_id":"804586","sn":"10163","time":"2017-11-06 10:10:27","name":null,"dev_code":"fzzx_1","row_number":"127"},{"dev_id":"804587","sn":"0682","time":"2017-11-06 10:10:31","name":"\u6797\u79cb\u7389","dev_code":"fzzx_1","row_number":"128"},{"dev_id":"804588","sn":"0815","time":"2017-11-06 10:13:29","name":"\u674e\u83b9","dev_code":"fzzx_2","row_number":"129"},{"dev_id":"804589","sn":"0875","time":"2017-11-06 10:13:40","name":"\u6731\u831c","dev_code":"fzzx_1","row_number":"130"},{"dev_id":"804590","sn":"10057","time":"2017-11-06 10:14:51","name":null,"dev_code":"fzzx_2","row_number":"131"},{"dev_id":"804591","sn":"0151","time":"2017-11-06 10:14:58","name":"\u5218\u5c0f\u80fd","dev_code":"fzzx_2","row_number":"132"},{"dev_id":"804592","sn":"0802","time":"2017-11-06 10:15:04","name":"\u7f57\u56fd\u7f8e","dev_code":"fzzx_1","row_number":"133"},{"dev_id":"804593","sn":"1089","time":"2017-11-06 10:16:34","name":"\u9648\u5a77","dev_code":"fzzx_1","row_number":"134"},{"dev_id":"804594","sn":"10142","time":"2017-11-06 10:16:34","name":null,"dev_code":"fzzx_2","row_number":"135"},{"dev_id":"804595","sn":"0825","time":"2017-11-06 10:16:36","name":"\u80e1\u56fd\u8f69","dev_code":"fzzx_1","row_number":"136"},{"dev_id":"804596","sn":"0030","time":"2017-11-06 10:16:37","name":"\u9b4f\u6654\u9a85","dev_code":"fzzx_2","row_number":"137"},{"dev_id":"804597","sn":"10191","time":"2017-11-06 10:16:40","name":null,"dev_code":"fzzx_1","row_number":"138"},{"dev_id":"804598","sn":"0102","time":"2017-11-06 10:17:45","name":"\u9648\u4e39\u9752","dev_code":"fzzx_2","row_number":"139"},{"dev_id":"804599","sn":"1024","time":"2017-11-06 10:17:55","name":"\u674e\u73ae\u529b","dev_code":"fzzx_2","row_number":"140"},{"dev_id":"804600","sn":"0936","time":"2017-11-06 10:18:36","name":"\u9ec4\u5609\u6167","dev_code":"fzzx_2","row_number":"141"},{"dev_id":"804601","sn":"10089","time":"2017-11-06 10:18:41","name":null,"dev_code":"fzzx_2","row_number":"142"},{"dev_id":"804602","sn":"0981","time":"2017-11-06 10:19:32","name":"\u8c2d\u9896\u5c27","dev_code":"fzzx_2","row_number":"143"},{"dev_id":"804603","sn":"10018","time":"2017-11-06 10:21:17","name":null,"dev_code":"fzzx_1","row_number":"144"},{"dev_id":"804604","sn":"1097","time":"2017-11-06 10:21:18","name":"\u9648\u83b9","dev_code":"fzzx_2","row_number":"145"},{"dev_id":"804605","sn":"0801","time":"2017-11-06 10:21:22","name":"\u738b\u5e78\u5a1f","dev_code":"fzzx_1","row_number":"146"},{"dev_id":"804606","sn":"0762","time":"2017-11-06 10:21:25","name":"\u59dc\u83b9\u83b9","dev_code":"fzzx_1","row_number":"147"},{"dev_id":"804607","sn":"0773","time":"2017-11-06 10:21:54","name":"\u65b9\u71d5\u5a1c","dev_code":"fzzx_2","row_number":"148"},{"dev_id":"804608","sn":"1079","time":"2017-11-06 10:22:00","name":"\u674e\u601d\u803f","dev_code":"fzzx_2","row_number":"149"},{"dev_id":"804609","sn":"0822","time":"2017-11-06 10:22:05","name":"\u674e\u9752\u674f","dev_code":"fzzx_1","row_number":"150"},{"dev_id":"804610","sn":"0983","time":"2017-11-06 10:22:32","name":"\u6c6a\u96e8\u5a77","dev_code":"fzzx_2","row_number":"151"},{"dev_id":"804611","sn":"0735","time":"2017-11-06 10:22:35","name":"\u674e\u71d5\u660e","dev_code":"fzzx_2","row_number":"152"},{"dev_id":"804612","sn":"0109","time":"2017-11-06 10:22:43","name":"\u738b\u5029\u6021","dev_code":"fzzx_1","row_number":"153"},{"dev_id":"804613","sn":"10033","time":"2017-11-06 10:22:47","name":null,"dev_code":"fzzx_1","row_number":"154"},{"dev_id":"804614","sn":"1087","time":"2017-11-06 10:23:12","name":"\u5de6\u7426","dev_code":"fzzx_2","row_number":"155"},{"dev_id":"804615","sn":"10166","time":"2017-11-06 10:23:18","name":null,"dev_code":"fzzx_1","row_number":"156"},{"dev_id":"804616","sn":"0099","time":"2017-11-06 10:23:31","name":"\u6234\u5f69\u73b2","dev_code":"fzzx_1","row_number":"157"},{"dev_id":"804617","sn":"0478","time":"2017-11-06 10:24:08","name":"\u71d5\u534e\u541b","dev_code":"fzzx_2","row_number":"158"},{"dev_id":"804618","sn":"10063","time":"2017-11-06 10:25:41","name":null,"dev_code":"fzzx_2","row_number":"159"},{"dev_id":"804619","sn":"10162","time":"2017-11-06 10:25:56","name":null,"dev_code":"fzzx_2","row_number":"160"},{"dev_id":"804620","sn":"10048","time":"2017-11-06 10:26:14","name":null,"dev_code":"fzzx_2","row_number":"161"},{"dev_id":"804621","sn":"0062","time":"2017-11-06 10:27:21","name":"\u4f4d\u6d01\u96e8","dev_code":"fzzx_2","row_number":"162"},{"dev_id":"804622","sn":"10134","time":"2017-11-06 10:27:28","name":null,"dev_code":"fzzx_2","row_number":"163"},{"dev_id":"804623","sn":"0754","time":"2017-11-06 10:27:31","name":"\u51af\u4fca\u6770","dev_code":"fzzx_1","row_number":"164"},{"dev_id":"804624","sn":"0845","time":"2017-11-06 10:28:31","name":"\u9648\u5609\u534e","dev_code":"fzzx_2","row_number":"165"},{"dev_id":"804625","sn":"1100","time":"2017-11-06 10:28:39","name":"\u9648\u97f5\u4f0a","dev_code":"fzzx_1","row_number":"166"},{"dev_id":"804626","sn":"1104","time":"2017-11-06 10:28:38","name":"\u9648\u73ae\u9716","dev_code":"fzzx_2","row_number":"167"},{"dev_id":"804627","sn":"10025","time":"2017-11-06 10:28:43","name":null,"dev_code":"fzzx_1","row_number":"168"},{"dev_id":"804628","sn":"0876","time":"2017-11-06 10:28:45","name":"\u5ed6\u6885\u4f36","dev_code":"fzzx_1","row_number":"169"},{"dev_id":"804629","sn":"0005","time":"2017-11-06 08:48:30","name":"\u5ed6\u6dd1\u82f1","dev_code":"fzzx_1","row_number":"170"},{"dev_id":"804630","sn":"0125","time":"2017-11-06 09:39:49","name":"\u5434\u4e54","dev_code":"fzzx_2","row_number":"171"},{"dev_id":"804631","sn":"0954","time":"2017-11-06 10:30:35","name":"\u9ea6\u6587\u806a","dev_code":"fzzx_1","row_number":"172"},{"dev_id":"804632","sn":"10001","time":"2017-11-06 10:31:16","name":null,"dev_code":"fzzx_2","row_number":"173"}]';
//         $attendance = json_decode($attendance,true);
//         $devcode = 'fzzx';
//         $date = '2017-11-05';
        
        if (!$date) $this->ajaxReturn(['msg'=>'date日期参数错误！！']);
        if (!$devcode) $this->ajaxReturn(['msg'=>'dev_code参数错误！！']);
        
        if (!$attendance) {
            $msg = "[系统机]获取打卡记录失败";
            
            $this->repair_log($date,$msg, $devcode);
            $this->ajaxReturn($msg);
            exit();
        }
        
        $attendanceUserInfo= D('Account','Service')->append_user_field('sn',$attendance,'sn,dep_id,dep_name,ep_id as uid,ep_name as name');
        $attModel = D('Attendance');
        $namebox = [];
        foreach ($attendanceUserInfo as $k=>$v) {
            if (!$v['name']) continue;
            $namebox[$v['sn']]=$v['name'];    
            $attModel->write_to_db($v);//执行数据补充添加
        }
        //添加完毕，提取员工姓名，要执行数据重置
        $resetService = new ResetDataService();
        $i = 0;
        foreach ($namebox as $k=>$v){
            
            $reset_att = $resetService->reset_user_attday($v, $date);
            if ($reset_att) {
                $resetService->reset_user_apply($v, $date);
            }else{
                exit();
            }
            $i++;
        }
    
        F($cache_key,NULL);
        $count_name = count($attendanceUserInfo);
        $count_att = count($attendance);
        $count_err = $count_name-$i;
        $msg = "已接收{$count_att}条记录,共{$count_name}个员工;重置结果：成功[{$i}];失败[{$count_err}]";
        
        $this->repair_log($date,$msg, $devcode);
        $this->ajaxReturn($msg);
    }
    
    /**
     * 记录自动修复日志
     * @param unknown $date
     * @param unknown $msg
     * @param unknown $dev_code
     * date:2017年10月13日
     * author: EK_熊
     */
    public function repair_log($date,$msg,$dev_code){
        $add['info'] = "[$date]自动修复：".$msg;
        $add['created_time'] = CURRENT_TIME;
        $add['dev_code'] = $dev_code;
        M('AttendanceChecklog')->add($add);
    }
    
    
    /**
     * 配合模型方法collect_day，但打卡时间点比当前的时间小的时候，会执行
     * 通过合法性检查后，执行重置功能
     * 
     * date:2017年12月18日
     * author: EK_熊
     */
    public function rest_less(){
        
        $resetService = new ResetDataService();
        $reset_att = $resetService->reset_user_attday($name, $date);
    }
    
    
    public function analogData(){
       $day = date('Y-m-d');
/*         $attendance = '[
        {"dev_id":1,"name":"null","sn":"0568","time":"'.$day.' 09:14:01"}
            ]'; */

       $attendance = '[
       {"dev_id":"72879","time":"2017-11-23 22:36:42","name":"员工1","sn":"0859","dev_code":"fzzx_1","row_number":"1"},
       {"dev_id":"72879","time":"2017-11-23 22:37:42","name":"员工2","sn":"0859","dev_code":"fzzx_1","row_number":"1"},
       {"dev_id":"72879","time":"2017-11-23 22:38:42","name":"员工3","sn":"0859","dev_code":"fzzx_1","row_number":"1"},
       {"dev_id":"72879","time":"2017-11-23 22:39:42","name":"员工4","sn":"0859","dev_code":"fzzx_1","row_number":"1"}
           ]';

// $attendance = str_replace("\n",'',$attendance);
// $attendance = str_replace("\r",'',$attendance);
        $attendance = json_decode($attendance,true);
        return $attendance;
    }
    
    
    
    /**
     * 加班问候
     * 
     * date:2017年11月30日
     * author: EK_熊
     */
    public function overtime_alert(){
        $redis = new Redis();
//         $attendance = $this->analogData();//数据模拟
        $attendance = htmlspecialchars_decode(I('data'));
        $attendance = json_decode($attendance,true);

        $start_time=date('Y-m-d 22:00:00');
        $end_time = date('Y-m-d 08:00:00',strtotime("+1 day"));
        $cur_time = CURRENT_TIME;
        $day = date('Y-m-d');
        $prefix = "ot_".$day.":";
        $expire = 259200;//3天
        if ($cur_time < $start_time || $cur_time > $end_time) {
            $this->ajaxReturn('时间未到'); //时辰未到
            exit();
        }  
        
        $account = new AccountService();
        $attendance= $account->append_user_field('sn',$attendance,'sn,wx_openid,ep_name as name');
        $wechat = new WechatService();
        //先查找缓存是否有记录,夜间数据交互很少，一个循环搞定
        foreach ($attendance as $k=>$v){
            $sn_key = $prefix.$v['sn'];
            $check_redis = $redis->get($sn_key);
            if (!$check_redis){
               //发送客服消息
                $wechat->send_custom_message($v['wx_openid'], 'cce4314d9fd4807a');
                $setredis = $redis->set($sn_key,$v['name'],$expire);
            }
        }
        
    }


    
    

    
}